package com.example.hello.controller;

import com.example.hello.common.Result;
import com.example.hello.dto.UserDTO;
import com.example.hello.entity.User;
import com.example.hello.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "管理员用户管理", description = "管理员对用户进行管理的相关接口")
@RestController
@RequestMapping("/api/admin/user")
@PreAuthorize("hasRole('ADMIN')")
public class AdminUserController {

  @Autowired
  private UserService userService;

  /**
   * 分页查询用户列表
   */
  @Operation(summary = "分页查询用户列表", description = "管理员分页查询用户列表，支持多条件筛选")
  @ApiResponses(value = {
      @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = PageInfo.class))),
      @ApiResponse(responseCode = "403", description = "没有权限")
  })
  @GetMapping("/list")
  public Result<PageInfo<User>> getUserList(
      @Parameter(description = "用户查询条件") User user,
      @Parameter(description = "页码，默认为1") @RequestParam(defaultValue = "1") Integer pageNum,
      @Parameter(description = "每页记录数，默认为10") @RequestParam(defaultValue = "10") Integer pageSize) {
    PageInfo<User> pageInfo = userService.getUserList(user, pageNum, pageSize);
    return Result.success(pageInfo);
  }

  /**
   * 获取用户详情
   */
  @Operation(summary = "获取用户详情", description = "管理员获取指定用户的详细信息")
  @ApiResponses(value = {
      @ApiResponse(responseCode = "200", description = "获取成功"),
      @ApiResponse(responseCode = "404", description = "用户不存在"),
      @ApiResponse(responseCode = "403", description = "没有权限")
  })
  @GetMapping("/{userId}")
  public Result<User> getUserDetail(
      @Parameter(description = "用户ID", required = true) @PathVariable Long userId) {
    User user = userService.getUserById(userId);
    if (user == null) {
      return Result.error(404, "用户不存在");
    }
    return Result.success(user);
  }

  /**
   * 管理员修改用户信息
   */
  @Operation(summary = "修改用户信息", description = "管理员修改用户信息（除用户名和密码外）")
  @ApiResponses(value = {
      @ApiResponse(responseCode = "200", description = "修改成功"),
      @ApiResponse(responseCode = "400", description = "请求参数错误"),
      @ApiResponse(responseCode = "403", description = "没有权限"),
      @ApiResponse(responseCode = "404", description = "用户不存在")
  })
  @PutMapping("/{userId}")
  public Result<Void> updateUserInfo(
      @Parameter(description = "用户ID", required = true) @PathVariable Long userId,
      @Parameter(description = "用户信息", required = true) @RequestBody @Validated UserDTO userDTO) {
    
    // 设置用户ID，确保修改的是指定用户
    userDTO.setUserId(userId);
    
    // 检查用户是否存在
    User user = userService.getUserById(userId);
    if (user == null) {
        return Result.error(404, "用户不存在");
    }
    
    boolean success = userService.updateUserInfo(userDTO);
    if (success) {
        return Result.success();
    } else {
        return Result.error(500, "修改用户信息失败");
    }
  }
}